package com.isoft.service.gate.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.dao.CommonMetaDao;
import com.isoft.dao.gate.BoardingGateDao;
import com.isoft.entity.CommonMeta;
import com.isoft.entity.ball.Blls;
import com.isoft.entity.flight.FlightInfo;
import com.isoft.entity.gate.Gate;
import com.isoft.service.gate.BoardingGateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 登机门服务层实现类
 */
@Service
public class BoardingGateServiceImpl extends ServiceImpl<BoardingGateDao, Gate> implements BoardingGateService {
    @Autowired
    private BoardingGateDao boardingGateDao;
    @Autowired
    private CommonMetaDao commonMetaDao;
    @Autowired
    private CommonMeta commonMeta;

    @Override
    public List<Gate> findAllBoardingGate() {
        return boardingGateDao.findAllBoardingGate();
    }

    @Override
    public List<Gate> findBoardingGateBy(Integer METAID, String CODE) {
        return (METAID != null) ? boardingGateDao.findBoardingGateBy(METAID,CODE) : Collections.emptyList();
    }

    @Override
    public Boolean deleteBoardingGate(Integer ID) {
        if(ID !=null){
            Gate info = boardingGateDao.selectBoardingGateByID(ID);
            int result = boardingGateDao.deleteBoardingGate(ID);
            if(result>0){
                commonMetaDao.deleteCommonMeta(info.getMETAID());
            }
            return result>0;
        }
        return false;
    }

    @Override
    public Boolean updateBoardingGate(Gate gate) {
        return boardingGateDao.updateBoardingGate(gate)>0;
    }

    @Override
    @Transactional // 确保这些操作在一个事务中执行
    public Boolean insertBoardingGate(Gate gate) {
        try {
            //            因为是外键关联，所以先要插入meta表
            if (commonMetaDao.selectAllSEQN(gate.getMETAID()) != null) {
                return false;
            }
            commonMeta.setSEQN(gate.getMETAID());
            commonMeta.setCREATETIME(new Date());
            commonMeta.setSNDR("FIMS");
            commonMeta.setDDTM(new Date());
            commonMeta.setTYPE("DFME");
            commonMeta.setSTYP("GTLS");
            commonMetaDao.insertCommonMeta(commonMeta);
            boardingGateDao.enableIdentityInsert();
            int result = boardingGateDao.insertBoardingGate(gate);
            return result > 0;
        } finally {
            boardingGateDao.disableIdentityInsert();
        }
    }



    public IPage<Gate> getBllsInfosPage(int currentPage, int pageSize) {
        try {
            // 构建分页对象
            Page<Gate> page = new Page<>(currentPage, pageSize);

            LambdaQueryWrapper<Gate> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(Gate::getMETAID, Gate::getID, Gate::getGTNO, Gate::getGATEID, Gate::getCODE, Gate::getESTR, Gate::getEEND,Gate::getBTSC,Gate::getVALID);
            queryWrapper.orderByDesc(Gate::getID); // 按ID降序排序
            Page<Gate> gatePage = boardingGateDao.selectPage(page, queryWrapper);
            // 封装返回
            return gatePage;
        } catch (Exception e) {
            // 异常处理逻辑，此处可根据需要抛出异常或处理后返回默认值
            throw new RuntimeException("获取数据失败：" + e.getMessage());
        }
    }

}
